const app = getApp();
Page({
    data: {
        userData: [],            // 用户数据 
        goodsDetail: [],         // 商品详情数据 
        hintText:'',             // 弹窗提示文字
        btnType:'',              // 弹窗类型
        item: '../../img/img1.png',

        goods_id:0,              // 商品id
    },
    // 弹窗事件
    buyCallback(e) {
        this.dialog.showDialog();
    },
    /** 
     * 生命周期函数--监听页面加载 
     */
    onLoad: function (options) {
        var self = this;

        this.setData({
            goods_id: options.id,
            this_id: app.globalData.User_id,
        })

        this.dialog = this.selectComponent("#dialog");

        if (options.id) {
            wx.getStorage({
                key: 'user',
                success: function (res) {
                    self.setData({
                        userData: res.data
                    })
                    wx.request({
                        url: app.url.requestUrl + '/entrust/selldetails',
                        method: 'POST',
                        data: {
                            openid: res.data.openid,
                            token: res.data.token,
                            id: options.id
                        },
                        success: function (data) {
                            var list = data.data;
                            if (list.code == 1) {
                                self.setData({
                                    goodsDetail: list.data,
                                })
                            }
                        }
                    })
                },
            })
        }
    },
    // 联系用户
    contact(e) {
        var self = this;
        var url = "../my-chat/my-chat?user_id=goodsDetail.user_id";
        var this_id = this.data.this_id;
        var user_id = this.data.goodsDetail.user_id;
        var auction_id = this.data.auction_id;
        var name = this.data.goodsDetail.nick_name;
        if (this_id == user_id) {
            wx.showToast({
                image: "../../img/warning.png",
                title: '该商家是自己'
            })
            return false;
        }

        if (auction_id) {
            var chatRoomId = "chatRoomId" + auction_id;
            RongIMLib.RongIMClient.getInstance().quitChatRoom(chatRoomId, {
                onSuccess: function () {
                    console.log("退出聊天室成功" + chatRoomId);
                },
                onError: function (error) {
                    console.log("退出聊天室失败")
                }
            });
        }

        wx.navigateTo({
            url: `../my-chat/my-chat?user_id=${user_id}&name=${name}`,
        })
    },
    // 弹窗 点击取消 
    __cancelEvent() {
        this.dialog.hideDialog();
    },
    // 弹窗 点击确认
    __confirmEvent() {
        this.dialog.hideDialog();
        // 发送消息给卖家，请求确认...
        this.applyGiveSell();
    },
    // 发送帮他卖请求
    applyGiveSell(){
        var self = this;
        var url = "/entrust/helpapply";
        var goods_id = this.data.goods_id;

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        goods_id: goods_id,
                        type:1
                    },
                    success: function (data) {
                        var list = data.data;
                        if (list.code == 1) {
                            wx.showToast({
                                title: '申请成功',
                            })
                        }else{
                            wx.showToast({
                                image:"../../img/warning.png",
                                title: list.msg,
                            })
                        }
                    }
                })
            },
        })
    },
})